#include "mapcontainer.h"

#include "map.h"

/**
 * 26.03.12 - Steven Hartin - Original Creation
 */

CMapContainer *CMapContainer::getInstance()
{
    /** 
     * @static
     * @return CMapContainer* A reference to the static singleton
     */
    static CMapContainer s_instance;
    return &s_instance;
}

CMapContainer::CMapContainer()
    : m_pCurrentMap(nullptr)
{
    /** 
     * Constructor, ensure that the map is intiialised to a null ptr
     */
}

CMapContainer::~CMapContainer()
{
    /** 
     * Destructor, delete the memory of the current map.
     */
    unloadCurrentMap();
}

CMap *CMapContainer::getCurrentMap() const
{
    /**
     * @return CMap* The current loaded map
     */
    return m_pCurrentMap;
}

void CMapContainer::setCurrentMap(CMap *pMap)
{
    /**
     * @param CMap *pMap The map to assign as the current map.
     */
    if (isMapLoaded())
    {
        unloadCurrentMap();
    }
    m_pCurrentMap = pMap;
}

bool CMapContainer::isMapLoaded() const
{
    /**
     * @return bool Whether or not the map is currently loaded.
     */
    return m_pCurrentMap != nullptr;
}

void CMapContainer::unloadCurrentMap()
{
    /**
     * Unloads the current map and frees the memory
     */
    if (m_pCurrentMap != nullptr)
    {
        m_pCurrentMap->unload();
        delete m_pCurrentMap;
        m_pCurrentMap = nullptr;
    }
}